package com.github.xuemingyu.springboot.acticitycloud.service.impl;

import com.bidanet.bdcms.core.vo.Page;
import com.bidanet.hibernate.lambda.core.LambdaCriteria;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.github.xuemingyu.springboot.acticitycloud.dao.ActivityTemplateDao;
import com.github.xuemingyu.springboot.acticitycloud.driver.dingtalk.DingTalkService;
import com.github.xuemingyu.springboot.acticitycloud.entity.ActivityTemplate;
import com.github.xuemingyu.springboot.acticitycloud.service.ActivityTemplateService;
import com.taobao.api.ApiException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.github.xuemingyu.springboot.acticitycloud.entity.QActivityTemplate.activityTemplate;

@Service
@Transactional
public class ActivityTemplateServiceImpl extends BaseServiceImpl<ActivityTemplate> implements ActivityTemplateService {
    @Autowired
    private ActivityTemplateDao activityTemplateDao;
    @Autowired
    DingTalkService dingTalkService;

    @Override
    public void queryLike(ActivityTemplate activityTemplate, Page<ActivityTemplate> page) {
        LambdaCriteria<ActivityTemplate> lambdaCriteria = lambdaQuery.create(ActivityTemplate.class);
        if (StringUtils.isNotEmpty(activityTemplate.getModelName())) {
            lambdaCriteria.like(q -> {
                q.setModelName("%" + activityTemplate.getModelName() + "%");
            });
        }
        lambdaCriteria.eq(q -> {
            q.setDeleteTag(false);
        });
        List<ActivityTemplate> list = lambdaCriteria.orderDesc("modelId").list(page.getPageCurrent(), page.getPageSize());
        long count = lambdaCriteria.count();
        page.setTotal(count);
        page.setList(list);
    }

    @Override
    public void insert(ActivityTemplate activityTemplate) throws ApiException {
        checkString(activityTemplate.getModelName(), "模板名称不能为空");
        checkString(activityTemplate.getModelData(), "模板结构不能为空");
        OapiUserGetResponse userInfoByUserId = dingTalkService.getUserInfoByUserId(activityTemplate.getUserId());
        activityTemplate.setCreateName(userInfoByUserId.getName());
        activityTemplate.setDeleteTag(false);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        String createTime = simpleDateFormat.format(new Date());
        activityTemplate.setCreateTime(createTime);
        activityTemplateDao.save(activityTemplate);
    }

    @Override
    public void update(ActivityTemplate activityTemplate) {
        checkNull(activityTemplate.getModelId(), "模板id不能为空");
        checkString(activityTemplate.getModelName(), "模板名称不能为空");
        checkString(activityTemplate.getModelData(), "模板结构不能为空");
        ActivityTemplate activityTemplate1 = activityTemplateDao.findFirstByModelIdAndDeleteTag(activityTemplate.getModelId(), false);
        activityTemplate1.setModelData(activityTemplate.getModelData());
        activityTemplate1.setModelName(activityTemplate.getModelName());
        // activityTemplate1.setCreateName(activityTemplate.getCreateName());
        activityTemplate1.setUpdateTime(activityTemplate.getUpdateTime());
        activityTemplateDao.save(activityTemplate1);
    }

    @Override
    public void delete(String ids) {
        String[] idarry = ids.split(",");
        if (idarry.length <= 0) {
            errorMsg("id不能为空");
        }
        List<Long> idList = new ArrayList<>();
        for (String id : idarry) {
            idList.add(Long.valueOf(id));
        }
        List<ActivityTemplate> templates = activityTemplateDao.findActivityTemplatesByModelIdIsIn(idList);
        templates.forEach(q -> {
            q.setDeleteTag(true);
        });
        activityTemplateDao.save(templates);
    }

    @Override
    public ActivityTemplate findOneByModelId(Long modelId) {
        ActivityTemplate firstByModelId = activityTemplateDao.findFirstByModelIdAndDeleteTag(modelId, false);
        return firstByModelId;
    }

    @Override
    protected JpaRepository<ActivityTemplate, Long> getDao() {
        return activityTemplateDao;
    }
}
